package com.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dao.ExpenseDao;
import com.dao.UserDao;
import com.pojo.Expense;
import com.pojo.User;

@Controller
public class ExpenseController {
	@Resource
	private ExpenseDao eDao;
	@Resource
	private UserDao uDao;
	
	@RequestMapping(value="showAllExpense")
	public String getAll(Model model,HttpSession session){
		User u=(User) session.getAttribute("user");
		int d_id=u.getD_id();
		Subject subject = SecurityUtils.getSubject();
		boolean flag1=subject.hasRole("admin");
		boolean flag2=subject.hasRole("manager");
		boolean flag3=subject.hasRole("leader");
		if (flag1) {
			List<Expense> list =eDao.getAll();
				model.addAttribute("Expenses",list);
				return "showExpense";
		}else if(flag2){
				List<Expense> list =eDao.getOneAll(d_id);
				model.addAttribute("Expenses",list);
				return "showExpense";
		}else if(flag3){
				List<Expense> list =eDao.getOneAllB(d_id);
				model.addAttribute("Expenses",list);
				return "showExpense";
		}
		return "index";
	}
	
	@RequestMapping(value="application")
	public String getapplication(Model model,HttpSession session){
		User u=(User) session.getAttribute("user");
		model.addAttribute("id",u.getU_id());
		return "application";
	}
	
	@ResponseBody
	@RequestMapping(value="getOneExpense",method = RequestMethod.POST)
	public Expense getOne(int id){
		Expense expense =eDao.getOneExpense(id);
		return expense;
	}
	
	@ResponseBody
	@RequestMapping(value="delOneExpense")
	public String delOne(int id){
		boolean b= eDao.delOne(id);
		try{
            if (b){
                return "ok";
            }
            else {
                return "failed";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "failed";
        }
	}
	@ResponseBody
	@RequestMapping(value="addOneExpense",method = RequestMethod.POST)
	public String addOne(Expense expense){
		Date date = new Date();
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String time=dateFormat.format(date);
		expense.setE_date(time);
		User u=uDao.getName(expense.getU_id());
		String name=u.getU_nickname();
		expense.setU_nickname(name);
		expense.setE_checkman("无");
		boolean b= eDao.addOne(expense);
		try{
            if (b){
                return "ok";
            }
            else {
                return "failed";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "failed";
        }
	}
	
	@ResponseBody
	@RequestMapping(value="updateOneExpense", method = RequestMethod.POST)
	public String updateOne(Expense expense){
		boolean b= eDao.updateOne(expense);
		try{
            if (b){
                return "ok";
            }
            else {
                return "failed";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "failed";
        }
	}
	
	@ResponseBody
	@RequestMapping(value="approveOneExpense", method = RequestMethod.POST)
	public String approveOneExpense(int id,HttpSession session){
		User u=(User) session.getAttribute("user");
		boolean b= eDao.approveOneExpense(id,u.getU_nickname());
		try{
            if (b){
                return "ok";
            }
            else {
                return "failed";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "failed";
        }
	}
	
	@ResponseBody
	@RequestMapping(value="refuseOneExpense", method = RequestMethod.POST)
	public String refuseOneExpense(int id,HttpSession session){
		User u=(User) session.getAttribute("user");
		boolean b= eDao.refuseOneExpense(id,u.getU_nickname());
		try{
            if (b){
                return "ok";
            }
            else {
                return "failed";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "failed";
        }
	}
}
